import java.awt.*;
import javax.swing.*;



class BoardView extends JComponent {
	private static final Color TETRO_COLOR[] ={Color.magenta,Color.green,Color.red,
		Color.orange,Color.blue,Color.cyan,Color.yellow};
	
	private Board logicBoard;
	private Color bgColor=Color.BLACK;
	private int sizeOfTile; //dimension of an elementary tile
	
	public void paintComponent(Graphics g){
		calcSizeOfTile();
		paintBackground(g);
		paintLogicBoard(g);
		paintNextTetro(g);
		paintPlayerInfos(g);
		paintHeldTetro(g);
	}
	
	
	public void paintBackground(Graphics g){	//paint the background of the playing area (20*10)
		
		g.setColor(bgColor);
		g.fillRect(4*sizeOfTile,4*sizeOfTile,10*sizeOfTile,20*sizeOfTile);
		
	}
	
	public void paintLogicBoard(Graphics g){	// paint the current board TO FIX
	/*	byte mat[][]=logicBoard.getBoard();
		for(int i=0 ; i<mat.length; i++){
			for(int j=0 ; j<mat[0].length ; j++){
				if(mat[i][j]!=0){
					g.setColor(TETRO_COLOR[mat[i][j]-1]);
					g.fillRect((4+i)*sizeOfTile,(4+j)*sizeOfTile,sizeOfTile,sizeOfTile);
				}
			}
		}
	*/}
	
	public void paintTetro(Graphics g,int x, int y,Tetro t){	
		//paint tetro t at x,y location (beginning of the matrix)
		g.setColor(TETRO_COLOR[t.getId()-1]);					 
		byte mat[][]=t.getMatrix();
		for(int i=0 ; i<mat.length; i++){
			for(int j=0 ; j<mat[0].length ; j++){
				if(mat[i][j]!=0){
					g.fillRect(x+i*sizeOfTile,y+j*sizeOfTile,sizeOfTile,sizeOfTile);
				}
			}
		}
	}
	
	public void paintHeldTetro(Graphics g){		
		//paint the background of the holded tetro's box
		//paint the currently holded tetro, if exists
		g.setColor(bgColor);
		g.fillRect(0,0,4*sizeOfTile,4*sizeOfTile);
		Tetro t;
		try {
			t = logicBoard.getHeldTetro();
		} catch (NullPointerException e) {
			t=null;
		}
		if(t!=null){
			int x=0;
			if(t.getMatrix().length==3){
				x=(int) Math.floor(0.5*sizeOfTile);
			}
			paintTetro(g,x,x,t);
		}
	}
	
	public void paintNextTetro(Graphics g){
		//paint the background of the next tetro's box
		//paint the next tetro
		g.setColor(bgColor);
		g.fillRect(14*sizeOfTile,4*sizeOfTile,4*sizeOfTile,4*sizeOfTile);
		Tetro t;
		try {
			t = new Tetro(logicBoard.getNextTetros()[0]);
		} catch (NullPointerException e) {
			t=null;
		}
		if(t!=null){
			int x=14*sizeOfTile;
			int y=4*sizeOfTile;
			if(t.getMatrix().length==3){
				x = x + (int) Math.floor(0.5*sizeOfTile);
				y = y + (int) Math.floor(0.5*sizeOfTile);
			}
			paintTetro(g,x,y,t);
		}
	}
	
	public void paintPlayerInfos(Graphics g){			//display level, score and number of lines TO FIX
		g.setColor(Color.gray);
		g.fillRect(14*sizeOfTile,8*sizeOfTile,4*sizeOfTile,16*sizeOfTile);
		g.setColor(Color.black);
		/*g.drawString("Level\n"+logicBoard.getLevel(),15*sizeOfTile,12*sizeOfTile);
		g.drawString("Score\n"+logicBoard.getScore(),15*sizeOfTile,14*sizeOfTile);
		g.drawString("Lines\n"+logicBoard.getNbLinesDone(),15*sizeOfTile,16*sizeOfTile);*/
		
	}
	
	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
		this.repaint();
	}
	
	public int getSizeOfTile(){
		return sizeOfTile;
	}
	
	public void calcSizeOfTile(){ 
		//calculate the dimension of an elementary tile
		//depending on the size of this component
			double size1=Math.floor(this.getHeight()/24);
			double size2=Math.floor(this.getWidth()/18);
			if(size1<=size2){
				sizeOfTile=(int) size1;
			}else{
				sizeOfTile=(int) size2;
			}

	}
	
	public static void main(String[] args){
		JFrame myFrame = new JFrame("myFrame");
		BoardView bv =new BoardView();
		myFrame.setBounds(0,0, 800, 800);
		myFrame.setVisible(true);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		myFrame.add(bv);
	}
	
}
